/*
 * 1232. 短网址
 * 给出一个长网址，返回一个短网址。简单起见，忽视掉域名。
 * 
 * 你需要实现两个方法：
 * longToShort(url). 把一个长网址转换成一个短网址
 * shortToLong(url). 把一个短网址转换成一个以http://tiny.url/开头的长网址
 * 你可以任意设计算法，评测只关心两件事：
 * 1、短网址的key的长度应该等于6 （不算域名和反斜杠）。 可以使用的字符只有 [a-zA-Z0-9]. 比如: abcD9E
 * 2. 任意两个长的url不会对应成同一个短url，反之亦然。
 * 
 * 样例
 * 给定 url = http://www.lintcode.com/faq/?id=10, 给出下列答案（或者其它类似的）：
 * short_url = longToShort(url) // 可能返回 http://tiny.url/abcD9E
 * long_url = shortToLong(short_url) // 返回 http://www.lintcode.com/faq/?id=10
 * 你返回的应该是以 http://tiny.url/开头的key为6个字符的唯一的短网址. 比如 "http://tiny.url/abcD9E" 或者其它的.
 * 返回的长网址在这个例子中应该是 http://www.lintcode.com/faq/?id=10。
 * 
 * 2018.06.12 @jeyming
 */
package tiny_url_1232;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Tiny_url_1232 {
	List<String> longUrl = new ArrayList<String>();
	List<String> urlKey = new ArrayList<String>();
	
	public String createKey() {
		boolean judge = true;
		String result = "";
		while(judge == true) {
			judge = false;
			result = "";
			for(int i = 0; i < 6; ++i) {
				int rd = Math.random() >= 0.5 ? 1 : 0;
				if(rd == 0) {
					Random r = new Random();
					result += r.nextInt(9) + 1 + "";
				} else {
					char[] A_z = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
					Random sc = new Random();
					int sub = sc.nextInt(A_z.length);
					result += A_z[sub] + "";
				}
			}
			if(urlKey.contains(result) == true) {
				judge = true;
			}
		}
		return result;
	}
	
	public String createCustom(String long_url, String key) {
		// write your code here
		if((longUrl.contains(long_url) == false) && (urlKey.contains(key) ==false)) {
			longUrl.add(long_url);
			urlKey.add(key);
			return "http://tiny.url/" + key;
		} else if((longUrl.contains(long_url) == true) && (urlKey.contains(key) ==true) && ((longUrl.get(urlKey.indexOf(key)).equals(long_url) ==true) || (urlKey.get(longUrl.indexOf(long_url)).equals(key) ==true))) {	
			return "http://tiny.url/" + key;
		} else {
			return "error";
		}
	}

	/*
	 * @param long_url: a long url
	 * @return: a short url starts with http://tiny.url/
	 */
	public String longToShort(String long_url) {
		// write your code here
		if(longUrl.contains(long_url) == true) {
			String key = urlKey.get(longUrl.indexOf(long_url));
			return "http://tiny.url/" + key;
		} else {
			String key = createKey();
			return createCustom(long_url, key);
		}
	}

	/*
	 * @param short_url: a short url starts with http://tiny.url/
	 * @return: a long url
	 */
	public String shortToLong(String short_url) {
		// write your code here
		String key = "";
		int num = 0;
		int cnt = 0;
		for(int i = 0; i < short_url.length(); ++i) {
			if(short_url.charAt(i) == '/'){
				++cnt;
			}	
			if(cnt == 3) {
				num = i;
				break;
			}
		}
		for(int i = num + 1; i < short_url.length(); ++i) {
			key += short_url.charAt(i);
		}
		//System.out.println(key);
		if(urlKey.contains(key) != true) {
			return null;
		} else {
			return longUrl.get( urlKey.indexOf(key) );
		}
	}
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
